﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Common;
using QldtSdh.GUI.Utils;
using QldtSdh.Utils;

namespace QldtSdh.GUI.DuLieu
{
    public partial class FormQuanLyGiangVien_ChiTiet : BaseDialog
    {
        GiangVien giangVien;
        DonVi donVi;
        int countEntityToBeAdded = 0;
        public event Common.TListChangedEventHandler OnChanged;

        public FormQuanLyGiangVien_ChiTiet(DonVi donVi, GiangVien giangVien = null)
        {
            InitializeComponent();
            // nvtrung - add 20141224-1620
            cmbHocHam.DataSource = GiangVien.HocHamList;
            cmbHocVi.DataSource = GiangVien.HocViList;
            this.giangVien = giangVien;
            this.donVi = donVi;
            LoadDonVi();
            InitInterface();
        }

        void InitInterface()
        {
            if (giangVien != null)
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.EDIT, ENTITY_NAME.GIANGVIEN);
                chkTiepTucBoSung.Checked = false;
                chkTiepTucBoSung.Visible = false;
                txtMaGiangVienDaoTaoDaiHoc.Text = giangVien.MaGiangVienDaoTaoDaiHoc;
                txtHo.Text = giangVien.HoDem;
                txtTen.Text = giangVien.Ten;
                chkGioiTinh.Checked = giangVien.GioiTinh;
                txtNgaySinh.Text = giangVien.NgaySinh;
                txtDienThoai.Text = giangVien.SoDienThoai;
                txtEmail.Text = giangVien.Email;
                cmbHocHam.Text = giangVien.HocHam;
                cmbHocVi.Text =
                    giangVien.HocVi; // nvtrung - modified - 20141224 1620
                //string.IsNullOrEmpty(giangVien.HocVi) ? "Không" : giangVien.HocVi;
                txtDiaChi.Text = giangVien.DiaChi;
                txtMST.Text = giangVien.MaSoThue;
                chkThinhGiang.Checked = giangVien.LaThinhGiang;
                cmbDonVi.SelectedValue = giangVien.MaDonVi;
            }
            else
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.ADDNEW, ENTITY_NAME.GIANGVIEN);
                //var donViGiangDay = BizDonViDaoTao.GetByID(cmbDonVi.SelectedValue.ToString());
                txtMaGiangVienDaoTaoDaiHoc.Text = "";
                    //donViGiangDay != null ? BizGiangVien.GenerateIDGiangVien("DHT", donViGiangDay.MaDonVi) : "";
                chkTiepTucBoSung.Visible = true;
                chkTiepTucBoSung.Checked = true;
                txtHo.Text = "";
                txtTen.Text = "";
                chkGioiTinh.Checked = false;
                txtNgaySinh.Text = "";
                txtDienThoai.Text = "";
                txtEmail.Text = "";
                cmbHocHam.SelectedIndex = 0;
                cmbHocVi.SelectedIndex = 0;
                txtDiaChi.Text = "";
                txtMST.Text = "";
                chkThinhGiang.Checked = false;
                cmbDonVi.SelectedValue = donVi.MaDonViGUID;
            }
        }

        void LoadDonVi()
        {
            cmbDonVi.DataSource = BizDonViDaoTao.GetList();
            cmbDonVi.DisplayMember = "TenDonVi";
            cmbDonVi.ValueMember = "MaDonViGUID";
        }

        bool ValidateData()
        {
            errorNotify.Clear();
            if (cmbDonVi.SelectedValue == null)
            {
                errorNotify.SetError(cmbDonVi, "Không được bỏ trống đơn vị phụ trách");
                cmbDonVi.Focus();
                return false;
            }
            //if (txtMaGiangVienDaoTaoDaiHoc.Text.IsNullOrEmpty())
            //{
            //    errorNotify.SetError(txtMaGiangVienDaoTaoDaiHoc, string.Format(ERROR_CONSTANT_STRING.LOI_MARONG, ENTITY_NAME.GIANGVIEN));
            //    txtMaGiangVienDaoTaoDaiHoc.Focus();
            //    return false;
            //}
            if (txtHo.Text.IsNullOrEmpty())
            {
                errorNotify.SetError(txtHo, string.Format(ERROR_CONSTANT_STRING.LOI_FIELD_EMPTY, ENTITY_NAME.GIANGVIEN));
                txtHo.Focus();
                return false;
            }
            if (txtTen.Text.IsNullOrEmpty())
            {
                errorNotify.SetError(txtTen, string.Format(ERROR_CONSTANT_STRING.LOI_TENRONG, ENTITY_NAME.GIANGVIEN));
                txtTen.Focus();
                return false;
            }
            //DateTime i = DateTime.Now;
            //if (!DateTime.TryParse(txtNgaySinh.Text, out i))
            //{
            //    errorNotify.SetError(txtNgaySinh, string.Format("Lỗi ngày sinh"));
            //    txtTen.Focus();
            //    return false;
            //}
            return true;
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                if (giangVien != null)
                {
                    #region Cập nhật thông tin giảng viên
                    giangVien.HoDem = txtHo.Text;
                    giangVien.Ten = txtTen.Text;
                    giangVien.GioiTinh = chkGioiTinh.Checked;
                    giangVien.NgaySinh = txtNgaySinh.Text;
                    giangVien.SoDienThoai = txtDienThoai.Text;
                    giangVien.Email = txtEmail.Text;
                    giangVien.HocHam = cmbHocHam.Text;
                    giangVien.HocVi = cmbHocVi.Text;// == "Không" ? "" : cmbHocVi.Text;
                    giangVien.DiaChi = txtDiaChi.Text;
                    giangVien.MaDonVi = cmbDonVi.SelectedValue.ToString();
                    //giangVien.MaGiangVien = //txtMaGiangVienDaoTaoDaiHoc.Text;
                    //    donViGiangDay != null ? BizGiangVien.GenerateIDGiangVien("DHT", donViGiangDay.MaDonVi) : "";
                    giangVien.MaGiangVienDaoTaoDaiHoc = txtMaGiangVienDaoTaoDaiHoc.Text.Trim();
                    giangVien.LaThinhGiang = chkThinhGiang.Checked;
                    giangVien.MaSoThue = txtMST.Text;
                    var rs = BizGiangVien.Update(giangVien);
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                    else if (rs == CODE_RESULT_RETURN.MaTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtHo, string.Format(ERROR_CONSTANT_STRING.LOI_MATRUNG, ENTITY_NAME.GIANGVIEN));
                        txtHo.Focus();
                        return;
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtTen, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.GIANGVIEN));
                        txtTen.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_CAPNHAT, ENTITY_NAME.GIANGVIEN));
                        return;
                    }
                    #endregion
                }
                else
                {
                    #region Thêm mới giảng viên
                    giangVien = new GiangVien();
                    var donViGiangDay = BizDonViDaoTao.GetByID(cmbDonVi.SelectedValue.ToString());
                    giangVien.MaGiangVien = //txtMaGiangVienDaoTaoDaiHoc.Text;
                        donViGiangDay != null ? BizGiangVien.GenerateIDGiangVien("DHT", donViGiangDay.MaDonVi) : "";
                    giangVien.MaGiangVienDaoTaoDaiHoc = txtMaGiangVienDaoTaoDaiHoc.Text.Trim();
                    giangVien.HoDem = txtHo.Text;
                    giangVien.Ten = txtTen.Text;
                    giangVien.GioiTinh = chkGioiTinh.Checked;
                    giangVien.NgaySinh = txtNgaySinh.Text; ;
                    giangVien.SoDienThoai = txtDienThoai.Text;
                    giangVien.Email = txtEmail.Text;
                    giangVien.HocHam = cmbHocHam.Text;
                    giangVien.HocVi = cmbHocVi.Text;
                    giangVien.DiaChi = txtDiaChi.Text;
                    giangVien.MaDonVi = cmbDonVi.SelectedValue.ToString();
                    giangVien.LaThinhGiang = chkThinhGiang.Checked;
                    giangVien.MaSoThue = txtMST.Text;
                    var rs = BizGiangVien.Add(giangVien);
                    giangVien = null;
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        //Sau khi thêm thành công kiểm tra có cho nhập thêm hay không
                        countEntityToBeAdded++;
                        OnChanged(this, null);
                        if (chkTiepTucBoSung.Checked)
                        {
                            InitInterface();
                            txtMaGiangVienDaoTaoDaiHoc.Focus();
                        }
                        else
                        {
                            this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        }
                    }
                    else if (rs == CODE_RESULT_RETURN.MaTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtMaGiangVienDaoTaoDaiHoc, string.Format(ERROR_CONSTANT_STRING.LOI_MATRUNG, ENTITY_NAME.GIANGVIEN));
                        txtMaGiangVienDaoTaoDaiHoc.Focus();
                        return;
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtTen, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.GIANGVIEN));
                        txtTen.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_THEM, ENTITY_NAME.GIANGVIEN));
                        return;
                    }
                    #endregion
                }
            }
        }

        private void FormQuanLyGiangVien_ChiTiet_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (countEntityToBeAdded > 0)
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void cmbDonVi_SelectedIndexChanged(object sender, EventArgs e)
        {
            //var donViGiangDay = BizDonViDaoTao.GetByID(cmbDonVi.SelectedValue.ToString());
            //txtMaGiangVienDaoTaoDaiHoc.Text = donViGiangDay != null ? BizGiangVien.GenerateIDGiangVien("DHT", donViGiangDay.MaDonVi) : "";
        }
    }
}
